import { defineStore } from 'pinia';
import { ref } from 'vue';
import {
  listTask,
  getTask,
  addTask,
  updateTask,
  delTask,
  startTask,
  endTask, uploadTask,
} from '@/api/tasks';
import { ElMessage } from 'element-plus';

export const useTasksStore = defineStore('tasks', () => {
  // --- State ---
  const tasks = ref([]); // 任务列表
  const total = ref(0); // 任务总数
  const loading = ref(false); // 列表加载状态
  const currentTask = ref(null); // 当前查看或执行的任务详情

  // --- Actions ---

  /**
   * 获取任务列表 (支持分页和筛选)
   * @param {object} params - 查询参数 { pageNum, pageSize, ...filters }
   */
  const fetchTasks = async (params) => {
    loading.value = true;
    try {
      const response = await listTask(params);
      tasks.value = response.data.rows || [];
      total.value = response.data.total || 0;
    } catch (error) {
      ElMessage.error('获取任务列表失败！');
      console.error(error);
      tasks.value = [];
      total.value = 0;
    } finally {
      loading.value = false;
    }
  };

  /**
   * 获取单个任务详情
   * @param {string|number} taskId - 任务ID
   */
  const fetchTaskDetail = async (taskId) => {
    loading.value = true;
    try {
      const response = await getTask(taskId);
      currentTask.value = response.data.data;
      return response.data.data;
    } catch (error) {
      ElMessage.error('获取任务详情失败！');
      currentTask.value = null;
    } finally {
      loading.value = false;
    }
  };

  /**
   * 创建新任务
   * @param {object} taskData - 任务表单数据
   */
  const createTask = async (taskData) => {
    try {
      await addTask(taskData);
      ElMessage.success('新增任务成功！');
      // 成功后刷新列表
      await fetchTasks({ pageNum: 1, pageSize: 10 });
      return true;
    } catch (error) {
      ElMessage.error(error.message || '新增任务失败！');
      return false;
    }
  };

  /**
   * 更新任务
   * @param {object} taskData - 任务表单数据
   */
  const updateTaskData = async (taskData) => {
    try {
      await updateTask(taskData);
      ElMessage.success('更新任务成功！');
      await fetchTasks({ pageNum: 1, pageSize: 10 });
      return true;
    } catch (error) {
      ElMessage.error(error.message || '更新任务失败！');
      return false;
    }
  };

  /**
   * 删除任务
   * @param {string|number} taskId
   */
  const deleteTask = async (taskId) => {
    try {
      let {data} = await delTask(taskId);
      if(data.code === 200 || data.code === "200"){
        ElMessage.success('删除任务成功！');
      }else{
        ElMessage.error(`删除任务失败！任务${taskId}正在巡视中`);
      }
      await fetchTasks({ pageNum: 1, pageSize: 10 });
      return true;
    } catch (error) {
      ElMessage.error(error.message || '删除任务失败！');
      return false;
    }
  };

  /**
   * 启动一个任务
   * @param {string|number} taskId
   */
  const startNewTask = async (taskId) => {
    try {
      let {data} = await startTask(taskId);
      if(data.code === '200'){
        ElMessage.success('任务已启动！');
        return true;
      }else {
        ElMessage.error(data.msg);
        return false;
      }
    } catch (error) {
      ElMessage.error(error.message || '任务启动失败！');
      return false;
    }
  };

  const uploadCurrTask = async (taskId) => {
    let {data} = await uploadTask(taskId)
    return data
  }

  return {
    tasks,
    total,
    loading,
    currentTask,
    fetchTasks,
    fetchTaskDetail,
    createTask,
    updateTaskData,
    deleteTask,
    startNewTask,
    uploadCurrTask
  };
});